﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HandlingProcedures.DataBase;

namespace HandlingProcedures
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MedicalCard medicalCard;
        WindowAddingProcedures windowForAddingProcedures;

        public MainWindow()
        {
            InitializeComponent();
            App.MainWindow = this;
            medicalCard = new MedicalCard();
        }

        //создание окна для добавления процедуры
        private void AddProcedure_Click(object sender, RoutedEventArgs e)
        {
            if (medicalCard.Client == null)
            {
                MessageBox.Show("Клиент не выбран." + Environment.NewLine + "Выберите клиента и поворите попытку.");
                return;
            }
            if (windowForAddingProcedures == null || !windowForAddingProcedures.IsVisible)
            {
                windowForAddingProcedures = new WindowAddingProcedures(medicalCard);
                windowForAddingProcedures.Show();
            }            
        }

        //загрузка списка отдыхающих в санатории
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ListBoxClients.ItemsSource = App.DataBase.LoadClients();
        }

        //Действие при выборе отдыхающего
        private void ListBoxClients_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Client client = (Client)((ListBox)sender).SelectedItem;
            LoadDetailedInformation();
            try
            {
                medicalCard.Procedures = App.DataBase.LoadSchedule(client.Id);
                medicalCard.Client = client;
                DataGridProcedures.ItemsSource = medicalCard.Procedures;
            }
            catch (SystemException ex)
            {
                MessageBox.Show("Ошибка при загрузке назначенных процедур" + Environment.NewLine + ex.Message);
            }
        }

        private void LoadDetailedInformation()
        {
            try
            {
                DetailedInformation.DataContext = App.DataBase.GetInformationAboutClient(medicalCard.Client.Id);
            }
            catch
            {
                labelCheckIn.Content = "//Не удалось загрузить данные";
                labelCheckOut.Content = "//Не удалось загрузить данные";
            }
        }

        private void ShowAssignedProcedures_Click(object sender, RoutedEventArgs e)
        {
            WindowShowProcedures showProcedure = new WindowShowProcedures();
            showProcedure.Show();
        }

        private void ButtonDeleteProcedures_Click(object sender, RoutedEventArgs e)
        {
            if (DataGridProcedures.SelectedItem != null)
            {
                try
                {
                    App.DataBase.DeleteProcedure(((Client)ListBoxClients.SelectedItem).Id, from pr in (IEnumerable<Object>)DataGridProcedures.SelectedItems select (MedicalProcedure)pr);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                medicalCard.Procedures = App.DataBase.LoadSchedule(medicalCard.Client.Id);
                DataGridProcedures.ItemsSource = medicalCard.Procedures;
            }
            else
            {
                MessageBox.Show("Ни одна процедура не выбрана");
            }
        }

        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public void UpdateProcedures()
        {
            this.DataGridProcedures.Items.Refresh();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            App.MainWindow = null;
        }
    }
}
